package com.qianchang.microfinance.view.dialog.alert;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.dm.library.utils.DensityUtil;
import com.dm.library.utils.StringUtils;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.utils.MoneyTextWatcher;
import com.qianchang.microfinance.view.dialog.DimengBaseDialog;

public class RechargeDialog extends DimengBaseDialog implements View.OnClickListener{
    private EditText etInput1;
    private RechargeDialog.OnDialogClickListener onDialogClickListener;
    private Dialog mDialog;

    public static RechargeDialog newInstance(String title) {
        Bundle bundle = new Bundle();
        bundle.putString(DMConstant.StoreConstant.Title, title);
        RechargeDialog dimengAlertDialog = new RechargeDialog();
        if (bundle != null) {
            dimengAlertDialog.setArguments(bundle);
        }
        return dimengAlertDialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mDialog = getDialog();
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setCanceledOnTouchOutside(false);
        return inflater.inflate(R.layout.recharge_ed_dialog, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        View view = getView();
        int width = DensityUtil.getScreenWidth(getActivity());
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = (int) (width * 0.85);
        view.setLayoutParams(params);

        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        TextView btn_agree = (TextView) view.findViewById(R.id.btn_dialog_agree);
        etInput1 = (EditText) view.findViewById(R.id.et_input1);
        etInput1.addTextChangedListener(new MoneyTextWatcher(etInput1));
        if (!TextUtils.isEmpty(agreeText)) {
            btn_agree.setText(agreeText);
        }
        TextView btn_dialog_cancel = (TextView) view.findViewById(R.id.btn_dialog_cancel);
        if (!TextUtils.isEmpty(notAgreeText)) {
            btn_dialog_cancel.setText(notAgreeText);
        }

        Bundle arguments = getArguments();
        if (arguments != null) {
            String s_title = arguments.getString(DMConstant.StoreConstant.Title, DMConstant.StoreConstant.NULL);
            if (!StringUtils.isEmptyOrNull(s_title)) {
                tv_title.setText(s_title);
            }
        }
        btn_agree.setClickable(true);
        btn_agree.setOnClickListener(this);
        btn_dialog_cancel.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dialog_agree:
                if (onDialogClickListener != null) {
                    onDialogClickListener.agree(etInput1.getEditableText().toString().trim());
                }
                break;
            case R.id.btn_dialog_cancel:
                if (onDialogClickListener != null) {
                    dismiss();
                    onDialogClickListener.cancel();
                }
                break;
        }
    }

    /**
     * 受保护的方法
     *
     * @param type
     */
    protected void setInputType(int type) {
        etInput1.setInputType(type);
    }

    protected void setHint(CharSequence hint) {
        etInput1.setHint(hint);
    }

    protected void setMaxLength(int max) {
        etInput1.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
    }

    /**
     * 弹出键盘
     */
    private void showKeyboard() {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager m = (InputMethodManager) getActivity().getSystemService(Context
                        .INPUT_METHOD_SERVICE);
                m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 300);
    }

    public void setCanceledOnTouchOutside(boolean cancel) {
        if (mDialog != null) {
            mDialog.setCanceledOnTouchOutside(cancel);
        }
    }

    public void setOnDialogClickListener(RechargeDialog.OnDialogClickListener onDialogClickListener) {
        this.onDialogClickListener = onDialogClickListener;
    }

    public interface OnDialogClickListener {
        void agree(String input);

        void cancel();
    }
    
}
